package com.nxk.modules.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.nxk.modules.core.entity.SysUser;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.mgt.SessionManager;
import org.springframework.stereotype.Component;

import javax.security.auth.Subject;
import java.util.Date;

@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    /**
     * 插入时的填充策略
     *
     * @param metaObject
     */
    public void insertFill(MetaObject metaObject) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        this.setFieldValByName("createBy", sysUser.getUserNick(), metaObject);
        this.setFieldValByName("createDate", new Date(), metaObject);
        this.setFieldValByName("updateBy", sysUser.getUserNick(), metaObject);
        this.setFieldValByName("updateDate", new Date(), metaObject);
        this.setFieldValByName("status", "1", metaObject);
    }

    /**
     * 更新时的填充策略
     *
     * @param metaObject
     */
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", "test", metaObject);
        this.setFieldValByName("updateDate", new Date(), metaObject);
    }


}
